package cn.javabb.core.service.sys;

import cn.javabb.common.model.Tree;
import cn.javabb.common.utils.ListUtils;
import cn.javabb.core.base.BaseService;
import cn.javabb.core.constant.Constant;
import cn.javabb.core.entity.sys.MenuDO;
import cn.javabb.core.mapper.sys.MenuMapper;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
public class MenuService extends BaseService<MenuDO> {
    @Autowired
    private UserService userService;
    @Autowired
    private RolePermissionService rolePermissionService;

    /**
     * 获取用户的权限，如果是超级用户，获取所有的权限,否则只能获取用户角色的权限
     *
     * @param userId
     * @return
     * @author QINB
     */
    public List<MenuDO> listMenuByUserId(Integer userId) {
        List<MenuDO> permissionList;
        //如果是超级管理员，则获取所有
        if (userService.isAdmin(userId)) {
            permissionList = this.queryAll();
        } else {
            //其他用户获取自己权限的。
            JSONObject param = new JSONObject();
            param.put("sqlid", MenuMapper.class.getName() + ".listByUserId");
            param.put("userId", userId);
            permissionList = this.queryList(param);
        }
        return permissionList;
    }

    /**
     * 获取角色的菜单
     *
     * @param roleId
     * @return
     * @author QINB
     */
    public List<MenuDO> listByRoleId(Integer roleId) {
        List<MenuDO> permissionList;
        if (roleId == Constant.ADMIN_ROLE_ID) {
            permissionList = this.queryAll();
        } else {
            JSONObject param = new JSONObject();
            param.put("sqlid", MenuMapper.class.getName() + ".listByRoleId");
            param.put("roleId", roleId);
            permissionList = this.queryList(param);

        }
        return permissionList;
    }

    /**
     * 将权限封装成树
     * @return
     */
    public List<Tree> listTree() {
        List<Tree> treeList = new ArrayList<>();
        List<MenuDO> permissionList = this.queryAll();
        permissionList.forEach(p -> {
            Tree t = new Tree();
            t.setId(p.getId() + "");
            t.setParentId(p.getParentId() + "");
            t.setName(p.getMenuName() + "/" + p.getPermissionCode());
            treeList.add(t);
        });
        return treeList;

    }

    /**
     * 根据roleId获取带勾选的树
     *
     * @param roleId
     * @return
     */
    public List<Tree> checkTreeByRoleId(Integer roleId) {
        List<Tree> allTreeList = listTree();
        List<MenuDO> checkPermissionList = listByRoleId(roleId);
        for (Tree t : allTreeList) {
            for (MenuDO p : checkPermissionList) {
                if (t.getId().equals(p.getId() + "")) {
                    t.setChecked(true);
                }
            }
        }
        return allTreeList;
    }

    /**
     * 菜单列表
     * @param type
     * @return
     */
    public List<MenuDO> listMenusByType(Integer type) {
        Example ex = new Example(MenuDO.class);
        ex.createCriteria().andEqualTo("type", type);
        ex.orderBy("sort").desc();
        return this.queryListByExample(ex);
    }

    /**
     * 删除菜单权限
     *
     * @param ids
     * @return
     * @author QINB
     */
    public boolean delete(String ids) {
        List<String> idList = ListUtils.stringToListString(ids);
        int num = this.deleteByIds(MenuDO.class, "id", idList);
        return num > 0 ? true : false;
    }
}
